$(function () {
    //获取检查信息
    queryCheckInfo("201");
    //标签管理选中样式
    $('.label-nav-con li').click(function() {
        $(this).addClass('current').siblings().removeClass('current');
        var checkType = $(this).attr("id");
        queryCheckInfo(checkType)
    })
})

//获取费用数据信息列表
function queryCheckInfo(checkType){
    var str="";
    var colInfo;
    var checkLabel="";
    $("#contentInfo").empty();
    str+="<div class=\"border-radius4 bgWhite box-shadows\">";
    str+="<div class=\"scroll-box-con\">";
    str+="<div class=\"mb20px lh32px overflow add-btn-input\">";
    if(isCURD){
        str+="<p class=\"left cursor\">";
        str+="<span onclick='addCheckInfo()' class=\"white border-radius4 bgGreen inline-block pl15px pr15px\">" +
            "<img src='"+getRootPath_web()+"static/img/button_add.png'>新建</span>";
        str+="</p>";
    }
    str+="<p class=\"right\">共有<span id='totalCount' class=\"red\">0</span>条数据</p>";
    str+="</div>";
    str+="<div class=\"border1px border-radius4 relative mb40px\">";
    str+="<table class=\"layui-hide\" id=\"test\" lay-filter=\"test\"></table>";
    str+="</div>";
    str+="</div>";
    str+="</div>";
    $("#contentInfo").append(str);
    if(checkType=='201'){
        checkLabel="实验室检查类型";
    }else if(checkType=='202'){
        checkLabel="诊断病类型";
    }else {
        checkLabel="诊疗计划模板"
    }
    //如果是实验室检查类型
    colInfo=  [
        [ //表头
            {
                field: 'order',
                title: '序号',
                fixed: 'left',
                width: 80,
            }, {
            field: 'checkName',
            title: checkLabel,
        }, {
            field: 'operation',
            title: '操作',
            style: 'color:#1C9399;',
            fixed: 'right',
            width: 140,
            templet: function(res){
                console.log(res);
                if(isCURD){
                    return "<div class='table-list-btn'><span class='cursor' lay-event=\"edit\">编辑</span>|<span class='cursor' lay-event=\"del\">删除</span></div>"
                }else {
                    return "<div class='table-list-btn'><span style='color: gray'>编辑</span>|<span style='color: gray'>删除</span></div>"
                }

            }
        }
        ]
    ]
    layui.use(['table'], function() {
        var table = layui.table;
        //第一个实例
        table.render({
            elem: '#test',
            url:getRootPath_web()+'baseData/check/obtainCheckData',
            method:'post',
            where:{checkType:checkType},
            parseData: function(res){
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.codeDesc, //解析提示文本
                    "count": res.checkCount, //解析数据长度
                    "data": res.data //解析数据列表
                };
            },
            done: function(res, curr, count){
                $("#totalCount").html(count);
            },
            page: false //开启分页
            ,
            cols: colInfo
        });
        //点击事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            var checkNo = data.checkNo;
            if(obj.event === 'del'){
                deleteCheckInfo(checkNo);
            } else if(obj.event === 'edit'){
                updateCheckInfo(checkNo);
            }
        });
    });
}
//费用添加页面
function addCheckInfo() {
    var checkType =$(".current").attr("id");
    var  str="";
    var nameLabel="";
    var nameTip="";
    var nameMaxLength=0;
    if(checkType=='201'){
        nameLabel="实验室类型名称:";
        nameTip="请输入实验室类型";
        nameMaxLength=20;
    }else if(checkType=='202'){
        //如果是长护险费
        nameLabel="诊断病类型名称:";
        nameTip="请输入诊断病类型";
        nameMaxLength=50;
    }else {
        nameLabel="诊疗计划模板:";
        nameTip="请输入诊疗计划模板";
        nameMaxLength=200;
    }
    //如果是护理费用
    str+="<div class=\"pop-up\">";
        str+="<div>";
            str+="<div class=\"pop-title relative overflow mb15px\">";
                str+="<span class=\"ml15px\">新增</span>";
                str+="<p class=\"right cursor\"><img src='"+getRootPath_web()+"static/img/close_icon.png' class=\"block pop-close\"><p>";
            str+="</div>";
        if(checkType=='203'){
            str+="<div class=\"pop-form pop-form-bed mt30px mb30px\">";
            str+="<div class=\"layui-form-item\">";
            str+="<label class=\"layui-form-label border-box\" style=\"width: 115px;\"><span class=\"red\">*</span>"+nameLabel+"</label>";
            str+="<div class=\"layui-input-block\" style=\"margin-left: 115px;\">";
                str+="<textarea rows=\"4\" cols=\"8\" maxlength='"+nameMaxLength+"' placeholder='"+nameTip+"' class=\"block border1px border-radius4 border-box checkName-input\" style=\"width: 100%; padding: 5px;\"></textarea>";
            str+="</div>";
            str+="</div>";
        }else {
            str+="<div class=\"pop-form pop-form-bed mt30px mb30px\">";
            str+="<div class=\"layui-form-item\">";
            str+="<label class=\"layui-form-label border-box\" style=\"width: 115px;\"><span class=\"red\">*</span>"+nameLabel+"</label>";
            str+="<div class=\"layui-input-block\" style=\"margin-left: 115px;\">";
            str+="<input type=\"text\"  maxlength='"+nameMaxLength+"' placeholder='"+nameTip+"' class=\"layui-input checkName-input\">";
            str+="</div>";
            str+="</div>";
        }
        str+="</div>";
            str+="<div class=\"btn-con textCenter\">";
            str+="<span class=\"btn-one btn-cancel cursor\">取&nbsp;消</span>";
            str+="<span class=\"btn-two btn_submit cursor\">提&nbsp;交</span>";
            str+="</div>";
        str+="</div>";
    str+="</div>";
    layui.use(['layer'], function() {
        var layer = layui.layer;
        //弹层
        layer.open({
            type: 1,
            title: false, //不显示标题栏
            closeBtn: false,
            area: ['500px', ''],
            shade: 0.6,
            id: 'payCons', //设定一个id，防止重复弹出
            /*,btn: ['确认支付']*/
            btnAlign: 'c',
            moveType: 1, //拖拽模式，0或者1
            shadeClose: false, //设置允许点遮罩关闭
            content:str,
            success: function() {
                //关闭弹层
                $(".pop-close").click(function() {
                    layer.closeAll();
                });
                $(".btn-cancel").click(function() {
                    layer.closeAll();
                });
                $(".btn_submit").click(function () {
                    saveCheckInfo(nameLabel,checkType);
                })
            },
        });
    });
}
//保存检查信息
function saveCheckInfo(nameLabel,checkType){
    layui.use('layer', function(){
        var layer = layui.layer;
        //检查名称
        var checkName = $(".checkName-input").val();
        if(checkName==''){
            layer.msg(nameLabel+'不能为空！');
            return false;
        }
        var loading;
        $.ajax({
            url:getRootPath_web()
                +'baseData/check/saveCheckInfo',
            type:'post',
            dataType:'json',
            data:{"checkName":checkName,"checkType":checkType},
            beforeSend:function () {
                loading = layer.load(1, {
                    shade: [0.9,'#fff']
                    ,shadeClose:false
                });
            },
            success:function (result) {
                if(result.code=='0000'){
                    layer.confirm('添加成功', {
                        btn: ['确定'] //按钮
                        ,shadeClose: false
                        ,closeBtn:false
                    }, function(){
                        layer.closeAll();
                        queryCheckInfo(checkType);
                    })
                }else {
                    layer.confirm(result.codeDesc, {
                        btn: ['确定'] //按钮
                        ,shadeClose: false
                        ,closeBtn:false
                    }, function(){
                        layer.close(layer.index);
                        layer.close(loading);
                    })
                }
            }
        })
    });
}
//修改检查信息
function updateCheckInfo(checkNo) {
    $.ajax({
        url:getRootPath_web()+'baseData/check/queryCheckInfoDetail',
        type:'post',
        dataType:'json',
        data:{"checkNo":checkNo},
        success:function (result) {
            var checkType =result.checkType;
            var  str="";
            var nameLabel="";
            var nameTip="";
            var nameMaxLength=0;
            if(checkType=='201'){
                nameLabel="实验室类型名称:";
                nameTip="请输入实验室类型";
                nameMaxLength=20;
            }else if(checkType=='202'){
                //如果是长护险费
                nameLabel="诊断病类型名称:";
                nameTip="请输入诊断病类型";
                nameMaxLength=50;
            }else {
                nameLabel="诊疗计划模板:";
                nameTip="请输入诊疗计划模板";
                nameMaxLength=200;
            }
            //如果是护理费用
            str+="<div class=\"pop-up\">";
            str+="<div>";
            str+="<div class=\"pop-title relative overflow mb15px\">";
            str+="<span class=\"ml15px\">编辑</span>";
            str+="<p class=\"right cursor\"><img src='"+getRootPath_web()+"static/img/close_icon.png' class=\"block pop-close\"><p>";
            str+="</div>";
            if(checkType=='203'){
                str+="<div class=\"pop-form pop-form-bed mt30px mb30px\">";
                str+="<div class=\"layui-form-item\">";
                str+="<label class=\"layui-form-label border-box\" style=\"width: 115px;\"><span class=\"red\">*</span>"+nameLabel+"</label>";
                str+="<div class=\"layui-input-block\" style=\"margin-left: 115px;\">";
                str+="<textarea rows=\"4\" cols=\"8\" maxlength='"+nameMaxLength+"' placeholder='"+nameTip+"' class=\"block border1px border-radius4 border-box checkName-input\" style=\"width: 100%; padding: 5px;\">"+result.checkName+"</textarea>";
                str+="</div>";
                str+="</div>";
            }else {
                str+="<div class=\"pop-form pop-form-bed mt30px mb30px\">";
                str+="<div class=\"layui-form-item\">";
                str+="<label class=\"layui-form-label border-box\" style=\"width: 115px;\"><span class=\"red\">*</span>"+nameLabel+"</label>";
                str+="<div class=\"layui-input-block\" style=\"margin-left: 115px;\">";
                str+="<input type=\"text\" value='"+result.checkName+"'  maxlength='"+nameMaxLength+"' placeholder='"+nameTip+"' class=\"layui-input checkName-input\">";
                str+="</div>";
                str+="</div>";
            }
            str+="</div>";
            str+="<div class=\"btn-con textCenter\">";
            str+="<span class=\"btn-one btn-cancel cursor\">取&nbsp;消</span>";
            str+="<span class=\"btn-two btn_submit cursor\">修&nbsp;改</span>";
            str+="</div>";
            str+="</div>";
            str+="</div>";
            layui.use(['layer'], function() {
                var layer = layui.layer;
                //弹层
                layer.open({
                    type: 1,
                    title: false, //不显示标题栏
                    closeBtn: false,
                    area: ['500px', ''],
                    shade: 0.6,
                    id: 'payCons', //设定一个id，防止重复弹出
                    /*,btn: ['确认支付']*/
                    btnAlign: 'c',
                    moveType: 1, //拖拽模式，0或者1
                    shadeClose: false, //设置允许点遮罩关闭
                    content:str,
                    success: function() {
                        //关闭弹层
                        $(".pop-close").click(function() {
                            layer.closeAll();
                        });
                        $(".btn-cancel").click(function() {
                            layer.closeAll();
                        });
                        $(".btn_submit").click(function () {
                            saveUpdateFeeInfo(nameLabel,checkType,checkNo);
                        })
                    },
                });
            });
        }
    })
}
//保存修改后的费用信息
function saveUpdateFeeInfo(nameLabel,checkType,checkNo) {
    layui.use('layer', function(){
        var layer = layui.layer;
        //检查名称
        var checkName = $(".checkName-input").val();
        if(checkName==''){
            layer.msg(nameLabel+'不能为空！');
            return false;
        }
        var loading;
        // layer.confirm('确定修改吗？', {
        //     btn: ['确定','取消'] //按钮
        // }, function(){
        // }, function(){
        //     layer.close(layer.index);
        // });
        $.ajax({
            url:getRootPath_web()
                +'baseData/check/updateCheckInfo',
            type:'post',
            dataType:'json',
            data:{"checkName":checkName,"checkType":checkType,"checkNo":checkNo},
            beforeSend:function () {
                loading = layer.load(1, {
                    shade: [0.9,'#fff']
                    ,shadeClose:false
                });
            },
            success:function (result) {
                if(result.code=='0000'){
                    layer.confirm('修改成功', {
                        btn: ['确定'] //按钮
                        ,shadeClose: false
                        ,closeBtn:false
                    }, function(){
                        layer.closeAll();
                        queryCheckInfo(checkType);
                    })
                }else {
                    layer.confirm(result.codeDesc, {
                        btn: ['确定'] //按钮
                        ,shadeClose: false
                        ,closeBtn:false
                    }, function(){
                        layer.close(layer.index);
                        layer.close(loading);
                    })
                }
            }
        })
    });
}
//删除检查信息
function deleteCheckInfo(checkNo) {
    layui.use('layer', function(){
        var layer = layui.layer;
        layer.confirm('确定删除吗？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.ajax({
                url:getRootPath_web()
                    +'baseData/check/deleteCheckInfo',
                type:'post',
                dataType:'json',
                data:{"checkNo":checkNo},
                success:function (result) {
                    if(result.code=='0000'){
                        layer.confirm('删除成功', {
                            btn: ['确定'] //按钮
                            ,shadeClose: false
                        }, function(){
                            layer.closeAll();
                            queryCheckInfo(result.checkType);
                        })
                    }else {
                        layer.confirm(result.codeDesc, {
                            btn: ['确定'] //按钮
                            ,shadeClose: false
                        }, function(){
                            layer.close(layer.index);
                        })
                    }
                }
            })
        }, function(){
            layer.close(layer.index);
        });
    });
}